
<!DOCTYPE HTML>
<html lang="zh-hans" >
    <head>
        <meta charset="UTF-8">
        <meta content="text/html; charset=utf-8" http-equiv="Content-Type">
        <title>Javascript教程 - 同源限制 - 小白AI.易名</title>
        <meta http-equiv="X-UA-Compatible" content="IE=edge" />
        <meta name="description" content="Javascript教程"><meta name="keywords" content="小白AI.易名,xiaobaiai.net,Javascript,入门,教程,博客">
        <meta name="generator" content="GitBook 3.2.3">
        <meta name="author" content="小白AI.易名">
        
        
    
    <link rel="stylesheet" href="../../gitbook/style.css">

    
            
                
                <link rel="stylesheet" href="../../gitbook/gitbook-plugin-chapter-fold/chapter-fold.css">
                
            
                
                <link rel="stylesheet" href="../../gitbook/gitbook-plugin-back-to-top-button/plugin.css">
                
            
                
                <link rel="stylesheet" href="../../gitbook/gitbook-plugin-search-pro/search.css">
                
            
                
                <link rel="stylesheet" href="../../gitbook/gitbook-plugin-page-footer-ex/style/plugin.css">
                
            
                
                <link rel="stylesheet" href="../../gitbook/gitbook-plugin-highlight/website.css">
                
            
                
                <link rel="stylesheet" href="../../gitbook/gitbook-plugin-search/search.css">
                
            
                
                <link rel="stylesheet" href="../../gitbook/gitbook-plugin-fontsettings/website.css">
                
            
        

    

    
        
    
        
    
        
    
        
    
        
    
        
    

        
    
    
    
    <meta name="HandheldFriendly" content="true"/>
    <meta name="viewport" content="width=device-width, initial-scale=1, user-scalable=no">
    <meta name="apple-mobile-web-app-capable" content="yes">
    <meta name="apple-mobile-web-app-status-bar-style" content="black">
    <link rel="apple-touch-icon-precomposed" sizes="152x152" href="../../gitbook/images/apple-touch-icon-precomposed-152.png">
    <link rel="shortcut icon" href="../../gitbook/images/favicon.ico" type="image/x-icon">

    
    <link rel="next" href="cors.html" />
    
    
    <link rel="prev" href="xmlhttprequest.html" />
    

    
        <link rel="shortcut icon" href='../../assets/images/favicon.ico' type="image/x-icon">
    
    
        <link rel="bookmark" href='../../assets/images/favicon.ico' type="image/x-icon">
    
    
        <link rel="apple-touch-icon" href='../../assets/images/apple-touch-icon.png'>
    
    
        
        <link rel="apple-touch-icon" sizes="120x120" href="../../assets/images/apple-touch-icon-120x120.png">
        
        <link rel="apple-touch-icon" sizes="180x180" href="../../assets/images/apple-touch-icon-180x180.png">
        
    

    </head>
    <body>
        
<div class="book">
    <div class="book-summary">
        
            
<div id="book-search-input" role="search">
    <input type="text" placeholder="输入并搜索" />
</div>

            
                <nav role="navigation">
                


<ul class="summary">
    
    

    

    
        
        
    
        <li class="chapter " data-level="1.1" data-path="../../">
            
                <a href="../../">
            
                    
                    简介
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.2" data-path="../basic/">
            
                <a href="../basic/">
            
                    
                    入门篇
            
                </a>
            

            
            <ul class="articles">
                
    
        <li class="chapter " data-level="1.2.1" data-path="../basic/introduction.html">
            
                <a href="../basic/introduction.html">
            
                    
                    导论
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.2.2" data-path="../basic/history.html">
            
                <a href="../basic/history.html">
            
                    
                    历史
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.2.3" data-path="../basic/grammar.html">
            
                <a href="../basic/grammar.html">
            
                    
                    基本语法
            
                </a>
            

            
        </li>
    

            </ul>
            
        </li>
    
        <li class="chapter " data-level="1.3" data-path="../types/">
            
                <a href="../types/">
            
                    
                    数据类型
            
                </a>
            

            
            <ul class="articles">
                
    
        <li class="chapter " data-level="1.3.1" data-path="../types/general.html">
            
                <a href="../types/general.html">
            
                    
                    概述
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.3.2" data-path="../types/null-undefined-boolean.html">
            
                <a href="../types/null-undefined-boolean.html">
            
                    
                    null,undefined和布尔值
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.3.3" data-path="../types/number.html">
            
                <a href="../types/number.html">
            
                    
                    数值
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.3.4" data-path="../types/string.html">
            
                <a href="../types/string.html">
            
                    
                    字符串
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.3.5" data-path="../types/object.html">
            
                <a href="../types/object.html">
            
                    
                    对象
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.3.6" data-path="../types/function.html">
            
                <a href="../types/function.html">
            
                    
                    函数
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.3.7" data-path="../types/array.html">
            
                <a href="../types/array.html">
            
                    
                    数组
            
                </a>
            

            
        </li>
    

            </ul>
            
        </li>
    
        <li class="chapter " data-level="1.4" data-path="../operators/">
            
                <a href="../operators/">
            
                    
                    运算符
            
                </a>
            

            
            <ul class="articles">
                
    
        <li class="chapter " data-level="1.4.1" data-path="../operators/arithmetic.html">
            
                <a href="../operators/arithmetic.html">
            
                    
                    算术运算符
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.4.2" data-path="../operators/comparison.html">
            
                <a href="../operators/comparison.html">
            
                    
                    比较运算符
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.4.3" data-path="../operators/boolean.html">
            
                <a href="../operators/boolean.html">
            
                    
                    布尔运算符
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.4.4" data-path="../operators/bit.html">
            
                <a href="../operators/bit.html">
            
                    
                    二进制位运算符
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.4.5" data-path="../operators/priority.html">
            
                <a href="../operators/priority.html">
            
                    
                    其他运算符，运算顺序
            
                </a>
            

            
        </li>
    

            </ul>
            
        </li>
    
        <li class="chapter " data-level="1.5" data-path="../features/">
            
                <a href="../features/">
            
                    
                    语法专题
            
                </a>
            

            
            <ul class="articles">
                
    
        <li class="chapter " data-level="1.5.1" data-path="../features/conversion.html">
            
                <a href="../features/conversion.html">
            
                    
                    数据类型的转换
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.5.2" data-path="../features/error.html">
            
                <a href="../features/error.html">
            
                    
                    错误处理机制
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.5.3" data-path="../features/style.html">
            
                <a href="../features/style.html">
            
                    
                    编程风格
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.5.4" data-path="../features/console.html">
            
                <a href="../features/console.html">
            
                    
                    console 对象与控制台
            
                </a>
            

            
        </li>
    

            </ul>
            
        </li>
    
        <li class="chapter " data-level="1.6" data-path="../stdlib/">
            
                <a href="../stdlib/">
            
                    
                    标准库
            
                </a>
            

            
            <ul class="articles">
                
    
        <li class="chapter " data-level="1.6.1" data-path="../stdlib/object.html">
            
                <a href="../stdlib/object.html">
            
                    
                    Object 对象
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.6.2" data-path="../stdlib/attributes.html">
            
                <a href="../stdlib/attributes.html">
            
                    
                    属性描述对象
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.6.3" data-path="../stdlib/array.html">
            
                <a href="../stdlib/array.html">
            
                    
                    Array 对象
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.6.4" data-path="../stdlib/wrapper.html">
            
                <a href="../stdlib/wrapper.html">
            
                    
                    包装对象
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.6.5" data-path="../stdlib/boolean.html">
            
                <a href="../stdlib/boolean.html">
            
                    
                    Boolean 对象
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.6.6" data-path="../stdlib/number.html">
            
                <a href="../stdlib/number.html">
            
                    
                    Number 对象
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.6.7" data-path="../stdlib/string.html">
            
                <a href="../stdlib/string.html">
            
                    
                    String 对象
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.6.8" data-path="../stdlib/math.html">
            
                <a href="../stdlib/math.html">
            
                    
                    Math 对象
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.6.9" data-path="../stdlib/date.html">
            
                <a href="../stdlib/date.html">
            
                    
                    Date 对象
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.6.10" data-path="../stdlib/regexp.html">
            
                <a href="../stdlib/regexp.html">
            
                    
                    RegExp 对象
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.6.11" data-path="../stdlib/json.html">
            
                <a href="../stdlib/json.html">
            
                    
                    JSON 对象
            
                </a>
            

            
        </li>
    

            </ul>
            
        </li>
    
        <li class="chapter " data-level="1.7" data-path="../oop/">
            
                <a href="../oop/">
            
                    
                    面向对象编程
            
                </a>
            

            
            <ul class="articles">
                
    
        <li class="chapter " data-level="1.7.1" data-path="../oop/new.html">
            
                <a href="../oop/new.html">
            
                    
                    实例对象与 New 命令
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.7.2" data-path="../oop/this.html">
            
                <a href="../oop/this.html">
            
                    
                    this 关键字
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.7.3" data-path="../oop/prototype.html">
            
                <a href="../oop/prototype.html">
            
                    
                    对象的继承
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.7.4" data-path="../oop/object.html">
            
                <a href="../oop/object.html">
            
                    
                    Object 对象的相关方法
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.7.5" data-path="../oop/strict.html">
            
                <a href="../oop/strict.html">
            
                    
                    严格模式
            
                </a>
            

            
        </li>
    

            </ul>
            
        </li>
    
        <li class="chapter " data-level="1.8" data-path="../async/">
            
                <a href="../async/">
            
                    
                    异步操作
            
                </a>
            

            
            <ul class="articles">
                
    
        <li class="chapter " data-level="1.8.1" data-path="../async/general.html">
            
                <a href="../async/general.html">
            
                    
                    概述
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.8.2" data-path="../async/timer.html">
            
                <a href="../async/timer.html">
            
                    
                    定时器
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.8.3" data-path="../async/promise.html">
            
                <a href="../async/promise.html">
            
                    
                    Promise 对象
            
                </a>
            

            
        </li>
    

            </ul>
            
        </li>
    
        <li class="chapter " data-level="1.9" data-path="../dom/">
            
                <a href="../dom/">
            
                    
                    DOM
            
                </a>
            

            
            <ul class="articles">
                
    
        <li class="chapter " data-level="1.9.1" data-path="../dom/general.html">
            
                <a href="../dom/general.html">
            
                    
                    概述
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.9.2" data-path="../dom/node.html">
            
                <a href="../dom/node.html">
            
                    
                    Node 接口
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.9.3" data-path="../dom/nodelist.html">
            
                <a href="../dom/nodelist.html">
            
                    
                    NodeList 接口，HTMLCollection 接口
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.9.4" data-path="../dom/parentnode.html">
            
                <a href="../dom/parentnode.html">
            
                    
                    ParentNode 接口，ChildNode 接口
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.9.5" data-path="../dom/document.html">
            
                <a href="../dom/document.html">
            
                    
                    Document 节点
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.9.6" data-path="../dom/element.html">
            
                <a href="../dom/element.html">
            
                    
                    Element 节点
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.9.7" data-path="../dom/attributes.html">
            
                <a href="../dom/attributes.html">
            
                    
                    属性的操作
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.9.8" data-path="../dom/text.html">
            
                <a href="../dom/text.html">
            
                    
                    Text 节点和 DocumentFragment 节点
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.9.9" data-path="../dom/css.html">
            
                <a href="../dom/css.html">
            
                    
                    CSS 操作
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.9.10" data-path="../dom/mutationobserver.html">
            
                <a href="../dom/mutationobserver.html">
            
                    
                    Mutation Observer API
            
                </a>
            

            
        </li>
    

            </ul>
            
        </li>
    
        <li class="chapter " data-level="1.10" data-path="../events/">
            
                <a href="../events/">
            
                    
                    事件
            
                </a>
            

            
            <ul class="articles">
                
    
        <li class="chapter " data-level="1.10.1" data-path="../events/eventtarget.html">
            
                <a href="../events/eventtarget.html">
            
                    
                    EventTarget 接口
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.10.2" data-path="../events/model.html">
            
                <a href="../events/model.html">
            
                    
                    事件模型
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.10.3" data-path="../events/event.html">
            
                <a href="../events/event.html">
            
                    
                    Event 对象
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.10.4" data-path="../events/mouse.html">
            
                <a href="../events/mouse.html">
            
                    
                    鼠标事件
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.10.5" data-path="../events/keyboard.html">
            
                <a href="../events/keyboard.html">
            
                    
                    键盘事件
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.10.6" data-path="../events/progress.html">
            
                <a href="../events/progress.html">
            
                    
                    进度事件
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.10.7" data-path="../events/form.html">
            
                <a href="../events/form.html">
            
                    
                    表单事件
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.10.8" data-path="../events/touch.html">
            
                <a href="../events/touch.html">
            
                    
                    触摸事件
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.10.9" data-path="../events/drag.html">
            
                <a href="../events/drag.html">
            
                    
                    拖拉事件
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.10.10" data-path="../events/common.html">
            
                <a href="../events/common.html">
            
                    
                    其他常见事件
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.10.11" data-path="../events/globaleventhandlers.html">
            
                <a href="../events/globaleventhandlers.html">
            
                    
                    GlobalEventHandlers 接口
            
                </a>
            

            
        </li>
    

            </ul>
            
        </li>
    
        <li class="chapter " data-level="1.11" data-path="./">
            
                <a href="./">
            
                    
                    浏览器模型
            
                </a>
            

            
            <ul class="articles">
                
    
        <li class="chapter " data-level="1.11.1" data-path="engine.html">
            
                <a href="engine.html">
            
                    
                    浏览器模型概述
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.11.2" data-path="window.html">
            
                <a href="window.html">
            
                    
                    window 对象
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.11.3" data-path="navigator.html">
            
                <a href="navigator.html">
            
                    
                    Navigatror 对象，Screen 对象
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.11.4" data-path="cookie.html">
            
                <a href="cookie.html">
            
                    
                    Cookie
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.11.5" data-path="xmlhttprequest.html">
            
                <a href="xmlhttprequest.html">
            
                    
                    XMLHttpRequest 对象
            
                </a>
            

            
        </li>
    
        <li class="chapter active" data-level="1.11.6" data-path="same-origin.html">
            
                <a href="same-origin.html">
            
                    
                    同源限制
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.11.7" data-path="cors.html">
            
                <a href="cors.html">
            
                    
                    CORS 通信
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.11.8" data-path="storage.html">
            
                <a href="storage.html">
            
                    
                    Storage 接口
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.11.9" data-path="history.html">
            
                <a href="history.html">
            
                    
                    History 对象
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.11.10" data-path="location.html">
            
                <a href="location.html">
            
                    
                    Location对象，URL对象，URLSearchParams 对象
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.11.11" data-path="arraybuffer.html">
            
                <a href="arraybuffer.html">
            
                    
                    ArrayBuffer 对象，Blob对象
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.11.12" data-path="file.html">
            
                <a href="file.html">
            
                    
                    File 对象，FileList 对象，FileReader 对象
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.11.13" data-path="form.html">
            
                <a href="form.html">
            
                    
                    表单，FormData 对象
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.11.14" data-path="indexeddb.html">
            
                <a href="indexeddb.html">
            
                    
                    IndexedDB API
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.11.15" data-path="webworker.html">
            
                <a href="webworker.html">
            
                    
                    web worker
            
                </a>
            

            
        </li>
    

            </ul>
            
        </li>
    
        <li class="chapter " data-level="1.12" data-path="../elements/">
            
                <a href="../elements/">
            
                    
                    附录:网页元素接口
            
                </a>
            

            
            <ul class="articles">
                
    
        <li class="chapter " data-level="1.12.1" data-path="../elements/a.html">
            
                <a href="../elements/a.html">
            
                    
                    a超链接
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.12.2" data-path="../elements/image.html">
            
                <a href="../elements/image.html">
            
                    
                    image图片
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.12.3" data-path="../elements/form.html">
            
                <a href="../elements/form.html">
            
                    
                    form表单
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.12.4" data-path="../elements/input.html">
            
                <a href="../elements/input.html">
            
                    
                    input文本框
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.12.5" data-path="../elements/button.html">
            
                <a href="../elements/button.html">
            
                    
                    button按钮
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.12.6" data-path="../elements/option.html">
            
                <a href="../elements/option.html">
            
                    
                    option选项
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.12.7" data-path="../elements/video.html">
            
                <a href="../elements/video.html">
            
                    
                    video,audio音视频
            
                </a>
            

            
        </li>
    

            </ul>
            
        </li>
    

    

    <li class="divider"></li>

    <li>
        <a href="https://www.gitbook.com" target="_blank" rel="nofollow" class="gitbook-link">
            本书使用 GitBook 发布
        </a>
    </li>
</ul>


                </nav>
            
        
    </div>

    <div class="book-body">
        
            <div class="body-inner">
                
                    

<div class="book-header" role="navigation">
    

    <!-- Title -->
    <h1>
        <i class="fa fa-circle-o-notch fa-spin"></i>
        <a href="../.." >同源限制</a>
    </h1>
</div>




                    <div class="page-wrapper" tabindex="-1" role="main">
                        <div class="page-inner">
                            
<div id="book-search-results">
    <div class="search-noresults">
    
<div id="book-search-results">
    <div class="search-noresults">
    
                                <section class="normal markdown-section">
                                
                                <h1 id="&#x540C;&#x6E90;&#x9650;&#x5236;">&#x540C;&#x6E90;&#x9650;&#x5236;</h1>
<p>&#x6D4F;&#x89C8;&#x5668;&#x5B89;&#x5168;&#x7684;&#x57FA;&#x77F3;&#x662F;&#x201C;&#x540C;&#x6E90;&#x653F;&#x7B56;&#x201D;&#xFF08;<a href="https://en.wikipedia.org/wiki/Same-origin_policy" target="_blank" rel="nofollow">same-origin policy</a>&#xFF09;&#x3002;&#x5F88;&#x591A;&#x5F00;&#x53D1;&#x8005;&#x90FD;&#x77E5;&#x9053;&#x8FD9;&#x4E00;&#x70B9;&#xFF0C;&#x4F46;&#x4E86;&#x89E3;&#x5F97;&#x4E0D;&#x5168;&#x9762;&#x3002;</p>
<h2 id="&#x6982;&#x8FF0;">&#x6982;&#x8FF0;</h2>
<h3 id="&#x542B;&#x4E49;">&#x542B;&#x4E49;</h3>
<p>1995&#x5E74;&#xFF0C;&#x540C;&#x6E90;&#x653F;&#x7B56;&#x7531; Netscape &#x516C;&#x53F8;&#x5F15;&#x5165;&#x6D4F;&#x89C8;&#x5668;&#x3002;&#x76EE;&#x524D;&#xFF0C;&#x6240;&#x6709;&#x6D4F;&#x89C8;&#x5668;&#x90FD;&#x5B9E;&#x884C;&#x8FD9;&#x4E2A;&#x653F;&#x7B56;&#x3002;</p>
<p>&#x6700;&#x521D;&#xFF0C;&#x5B83;&#x7684;&#x542B;&#x4E49;&#x662F;&#x6307;&#xFF0C;A &#x7F51;&#x9875;&#x8BBE;&#x7F6E;&#x7684; Cookie&#xFF0C;B &#x7F51;&#x9875;&#x4E0D;&#x80FD;&#x6253;&#x5F00;&#xFF0C;&#x9664;&#x975E;&#x8FD9;&#x4E24;&#x4E2A;&#x7F51;&#x9875;&#x201C;&#x540C;&#x6E90;&#x201D;&#x3002;&#x6240;&#x8C13;&#x201C;&#x540C;&#x6E90;&#x201D;&#x6307;&#x7684;&#x662F;&#x201C;&#x4E09;&#x4E2A;&#x76F8;&#x540C;&#x201D;&#x3002;</p>
<blockquote>
<ul>
<li>&#x534F;&#x8BAE;&#x76F8;&#x540C;</li>
<li>&#x57DF;&#x540D;&#x76F8;&#x540C;</li>
<li>&#x7AEF;&#x53E3;&#x76F8;&#x540C;</li>
</ul>
</blockquote>
<p>&#x4E3E;&#x4F8B;&#x6765;&#x8BF4;&#xFF0C;<code>http://www.example.com/dir/page.html</code>&#x8FD9;&#x4E2A;&#x7F51;&#x5740;&#xFF0C;&#x534F;&#x8BAE;&#x662F;<code>http://</code>&#xFF0C;&#x57DF;&#x540D;&#x662F;<code>www.example.com</code>&#xFF0C;&#x7AEF;&#x53E3;&#x662F;<code>80</code>&#xFF08;&#x9ED8;&#x8BA4;&#x7AEF;&#x53E3;&#x53EF;&#x4EE5;&#x7701;&#x7565;&#xFF09;&#xFF0C;&#x5B83;&#x7684;&#x540C;&#x6E90;&#x60C5;&#x51B5;&#x5982;&#x4E0B;&#x3002;</p>
<ul>
<li><code>http://www.example.com/dir2/other.html</code>&#xFF1A;&#x540C;&#x6E90;</li>
<li><code>http://example.com/dir/other.html</code>&#xFF1A;&#x4E0D;&#x540C;&#x6E90;&#xFF08;&#x57DF;&#x540D;&#x4E0D;&#x540C;&#xFF09;</li>
<li><code>http://v2.www.example.com/dir/other.html</code>&#xFF1A;&#x4E0D;&#x540C;&#x6E90;&#xFF08;&#x57DF;&#x540D;&#x4E0D;&#x540C;&#xFF09;</li>
<li><code>http://www.example.com:81/dir/other.html</code>&#xFF1A;&#x4E0D;&#x540C;&#x6E90;&#xFF08;&#x7AEF;&#x53E3;&#x4E0D;&#x540C;&#xFF09;</li>
<li><code>https://www.example.com/dir/page.html</code>&#xFF1A;&#x4E0D;&#x540C;&#x6E90;&#xFF08;&#x534F;&#x8BAE;&#x4E0D;&#x540C;&#xFF09;</li>
</ul>
<h3 id="&#x76EE;&#x7684;">&#x76EE;&#x7684;</h3>
<p>&#x540C;&#x6E90;&#x653F;&#x7B56;&#x7684;&#x76EE;&#x7684;&#xFF0C;&#x662F;&#x4E3A;&#x4E86;&#x4FDD;&#x8BC1;&#x7528;&#x6237;&#x4FE1;&#x606F;&#x7684;&#x5B89;&#x5168;&#xFF0C;&#x9632;&#x6B62;&#x6076;&#x610F;&#x7684;&#x7F51;&#x7AD9;&#x7A83;&#x53D6;&#x6570;&#x636E;&#x3002;</p>
<p>&#x8BBE;&#x60F3;&#x8FD9;&#x6837;&#x4E00;&#x79CD;&#x60C5;&#x51B5;&#xFF1A;A &#x7F51;&#x7AD9;&#x662F;&#x4E00;&#x5BB6;&#x94F6;&#x884C;&#xFF0C;&#x7528;&#x6237;&#x767B;&#x5F55;&#x4EE5;&#x540E;&#xFF0C;A &#x7F51;&#x7AD9;&#x5728;&#x7528;&#x6237;&#x7684;&#x673A;&#x5668;&#x4E0A;&#x8BBE;&#x7F6E;&#x4E86;&#x4E00;&#x4E2A; Cookie&#xFF0C;&#x5305;&#x542B;&#x4E86;&#x4E00;&#x4E9B;&#x9690;&#x79C1;&#x4FE1;&#x606F;&#xFF08;&#x6BD4;&#x5982;&#x5B58;&#x6B3E;&#x603B;&#x989D;&#xFF09;&#x3002;&#x7528;&#x6237;&#x79BB;&#x5F00; A &#x7F51;&#x7AD9;&#x4EE5;&#x540E;&#xFF0C;&#x53C8;&#x53BB;&#x8BBF;&#x95EE; B &#x7F51;&#x7AD9;&#xFF0C;&#x5982;&#x679C;&#x6CA1;&#x6709;&#x540C;&#x6E90;&#x9650;&#x5236;&#xFF0C;B &#x7F51;&#x7AD9;&#x53EF;&#x4EE5;&#x8BFB;&#x53D6; A &#x7F51;&#x7AD9;&#x7684; Cookie&#xFF0C;&#x90A3;&#x4E48;&#x9690;&#x79C1;&#x4FE1;&#x606F;&#x5C31;&#x4F1A;&#x6CC4;&#x6F0F;&#x3002;&#x66F4;&#x53EF;&#x6015;&#x7684;&#x662F;&#xFF0C;Cookie &#x5F80;&#x5F80;&#x7528;&#x6765;&#x4FDD;&#x5B58;&#x7528;&#x6237;&#x7684;&#x767B;&#x5F55;&#x72B6;&#x6001;&#xFF0C;&#x5982;&#x679C;&#x7528;&#x6237;&#x6CA1;&#x6709;&#x9000;&#x51FA;&#x767B;&#x5F55;&#xFF0C;&#x5176;&#x4ED6;&#x7F51;&#x7AD9;&#x5C31;&#x53EF;&#x4EE5;&#x5192;&#x5145;&#x7528;&#x6237;&#xFF0C;&#x4E3A;&#x6240;&#x6B32;&#x4E3A;&#x3002;&#x56E0;&#x4E3A;&#x6D4F;&#x89C8;&#x5668;&#x540C;&#x65F6;&#x8FD8;&#x89C4;&#x5B9A;&#xFF0C;&#x63D0;&#x4EA4;&#x8868;&#x5355;&#x4E0D;&#x53D7;&#x540C;&#x6E90;&#x653F;&#x7B56;&#x7684;&#x9650;&#x5236;&#x3002;</p>
<p>&#x7531;&#x6B64;&#x53EF;&#x89C1;&#xFF0C;&#x540C;&#x6E90;&#x653F;&#x7B56;&#x662F;&#x5FC5;&#x9700;&#x7684;&#xFF0C;&#x5426;&#x5219; Cookie &#x53EF;&#x4EE5;&#x5171;&#x4EAB;&#xFF0C;&#x4E92;&#x8054;&#x7F51;&#x5C31;&#x6BEB;&#x65E0;&#x5B89;&#x5168;&#x53EF;&#x8A00;&#x4E86;&#x3002;</p>
<h3 id="&#x9650;&#x5236;&#x8303;&#x56F4;">&#x9650;&#x5236;&#x8303;&#x56F4;</h3>
<p>&#x968F;&#x7740;&#x4E92;&#x8054;&#x7F51;&#x7684;&#x53D1;&#x5C55;&#xFF0C;&#x540C;&#x6E90;&#x653F;&#x7B56;&#x8D8A;&#x6765;&#x8D8A;&#x4E25;&#x683C;&#x3002;&#x76EE;&#x524D;&#xFF0C;&#x5982;&#x679C;&#x975E;&#x540C;&#x6E90;&#xFF0C;&#x5171;&#x6709;&#x4E09;&#x79CD;&#x884C;&#x4E3A;&#x53D7;&#x5230;&#x9650;&#x5236;&#x3002;</p>
<blockquote>
<p>&#xFF08;1&#xFF09; &#x65E0;&#x6CD5;&#x8BFB;&#x53D6;&#x975E;&#x540C;&#x6E90;&#x7F51;&#x9875;&#x7684; Cookie&#x3001;LocalStorage &#x548C; IndexedDB&#x3002;</p>
<p>&#xFF08;2&#xFF09; &#x65E0;&#x6CD5;&#x63A5;&#x89E6;&#x975E;&#x540C;&#x6E90;&#x7F51;&#x9875;&#x7684; DOM&#x3002;</p>
<p>&#xFF08;3&#xFF09; &#x65E0;&#x6CD5;&#x5411;&#x975E;&#x540C;&#x6E90;&#x5730;&#x5740;&#x53D1;&#x9001; AJAX &#x8BF7;&#x6C42;&#xFF08;&#x53EF;&#x4EE5;&#x53D1;&#x9001;&#xFF0C;&#x4F46;&#x6D4F;&#x89C8;&#x5668;&#x4F1A;&#x62D2;&#x7EDD;&#x63A5;&#x53D7;&#x54CD;&#x5E94;&#xFF09;&#x3002;</p>
</blockquote>
<p>&#x53E6;&#x5916;&#xFF0C;&#x901A;&#x8FC7; JavaScript &#x811A;&#x672C;&#x53EF;&#x4EE5;&#x62FF;&#x5230;&#x5176;&#x4ED6;&#x7A97;&#x53E3;&#x7684;<code>window</code>&#x5BF9;&#x8C61;&#x3002;&#x5982;&#x679C;&#x662F;&#x975E;&#x540C;&#x6E90;&#x7684;&#x7F51;&#x9875;&#xFF0C;&#x76EE;&#x524D;&#x5141;&#x8BB8;&#x4E00;&#x4E2A;&#x7A97;&#x53E3;&#x53EF;&#x4EE5;&#x63A5;&#x89E6;&#x5176;&#x4ED6;&#x7F51;&#x9875;&#x7684;<code>window</code>&#x5BF9;&#x8C61;&#x7684;&#x4E5D;&#x4E2A;&#x5C5E;&#x6027;&#x548C;&#x56DB;&#x4E2A;&#x65B9;&#x6CD5;&#x3002;</p>
<ul>
<li>window.closed</li>
<li>window.frames</li>
<li>window.length</li>
<li>window.location</li>
<li>window.opener</li>
<li>window.parent</li>
<li>window.self</li>
<li>window.top</li>
<li>window.window</li>
<li>window.blur()</li>
<li>window.close()</li>
<li>window.focus()</li>
<li>window.postMessage()</li>
</ul>
<p>&#x4E0A;&#x9762;&#x7684;&#x4E5D;&#x4E2A;&#x5C5E;&#x6027;&#x4E4B;&#x4E2D;&#xFF0C;&#x53EA;&#x6709;<code>window.location</code>&#x662F;&#x53EF;&#x8BFB;&#x5199;&#x7684;&#xFF0C;&#x5176;&#x4ED6;&#x516B;&#x4E2A;&#x5168;&#x90E8;&#x90FD;&#x662F;&#x53EA;&#x8BFB;&#x3002;&#x800C;&#x4E14;&#xFF0C;&#x5373;&#x4F7F;&#x662F;<code>location</code>&#x5BF9;&#x8C61;&#xFF0C;&#x975E;&#x540C;&#x6E90;&#x7684;&#x60C5;&#x51B5;&#x4E0B;&#xFF0C;&#x4E5F;&#x53EA;&#x5141;&#x8BB8;&#x8C03;&#x7528;<code>location.replace</code>&#x65B9;&#x6CD5;&#x548C;&#x5199;&#x5165;<code>location.href</code>&#x5C5E;&#x6027;&#x3002;</p>
<p>&#x867D;&#x7136;&#x8FD9;&#x4E9B;&#x9650;&#x5236;&#x662F;&#x5FC5;&#x8981;&#x7684;&#xFF0C;&#x4F46;&#x662F;&#x6709;&#x65F6;&#x5F88;&#x4E0D;&#x65B9;&#x4FBF;&#xFF0C;&#x5408;&#x7406;&#x7684;&#x7528;&#x9014;&#x4E5F;&#x53D7;&#x5230;&#x5F71;&#x54CD;&#x3002;&#x4E0B;&#x9762;&#x4ECB;&#x7ECD;&#x5982;&#x4F55;&#x89C4;&#x907F;&#x4E0A;&#x9762;&#x7684;&#x9650;&#x5236;&#x3002;</p>
<h2 id="cookie">Cookie</h2>
<p>Cookie &#x662F;&#x670D;&#x52A1;&#x5668;&#x5199;&#x5165;&#x6D4F;&#x89C8;&#x5668;&#x7684;&#x4E00;&#x5C0F;&#x6BB5;&#x4FE1;&#x606F;&#xFF0C;&#x53EA;&#x6709;&#x540C;&#x6E90;&#x7684;&#x7F51;&#x9875;&#x624D;&#x80FD;&#x5171;&#x4EAB;&#x3002;&#x5982;&#x679C;&#x4E24;&#x4E2A;&#x7F51;&#x9875;&#x4E00;&#x7EA7;&#x57DF;&#x540D;&#x76F8;&#x540C;&#xFF0C;&#x53EA;&#x662F;&#x6B21;&#x7EA7;&#x57DF;&#x540D;&#x4E0D;&#x540C;&#xFF0C;&#x6D4F;&#x89C8;&#x5668;&#x5141;&#x8BB8;&#x901A;&#x8FC7;&#x8BBE;&#x7F6E;<code>document.domain</code>&#x5171;&#x4EAB; Cookie&#x3002;</p>
<p>&#x4E3E;&#x4F8B;&#x6765;&#x8BF4;&#xFF0C;A &#x7F51;&#x9875;&#x7684;&#x7F51;&#x5740;&#x662F;<code>http://w1.example.com/a.html</code>&#xFF0C;B &#x7F51;&#x9875;&#x7684;&#x7F51;&#x5740;&#x662F;<code>http://w2.example.com/b.html</code>&#xFF0C;&#x90A3;&#x4E48;&#x53EA;&#x8981;&#x8BBE;&#x7F6E;&#x76F8;&#x540C;&#x7684;<code>document.domain</code>&#xFF0C;&#x4E24;&#x4E2A;&#x7F51;&#x9875;&#x5C31;&#x53EF;&#x4EE5;&#x5171;&#x4EAB; Cookie&#x3002;&#x56E0;&#x4E3A;&#x6D4F;&#x89C8;&#x5668;&#x901A;&#x8FC7;<code>document.domain</code>&#x5C5E;&#x6027;&#x6765;&#x68C0;&#x67E5;&#x662F;&#x5426;&#x540C;&#x6E90;&#x3002;</p>
<pre><code class="lang-javascript"><span class="hljs-comment">// &#x4E24;&#x4E2A;&#x7F51;&#x9875;&#x90FD;&#x9700;&#x8981;&#x8BBE;&#x7F6E;</span>
<span class="hljs-built_in">document</span>.domain = <span class="hljs-string">&apos;example.com&apos;</span>;
</code></pre>
<p>&#x6CE8;&#x610F;&#xFF0C;A &#x548C; B &#x4E24;&#x4E2A;&#x7F51;&#x9875;&#x90FD;&#x9700;&#x8981;&#x8BBE;&#x7F6E;<code>document.domain</code>&#x5C5E;&#x6027;&#xFF0C;&#x624D;&#x80FD;&#x8FBE;&#x5230;&#x540C;&#x6E90;&#x7684;&#x76EE;&#x7684;&#x3002;&#x56E0;&#x4E3A;&#x8BBE;&#x7F6E;<code>document.domain</code>&#x7684;&#x540C;&#x65F6;&#xFF0C;&#x4F1A;&#x628A;&#x7AEF;&#x53E3;&#x91CD;&#x7F6E;&#x4E3A;<code>null</code>&#xFF0C;&#x56E0;&#x6B64;&#x5982;&#x679C;&#x53EA;&#x8BBE;&#x7F6E;&#x4E00;&#x4E2A;&#x7F51;&#x9875;&#x7684;<code>document.domain</code>&#xFF0C;&#x4F1A;&#x5BFC;&#x81F4;&#x4E24;&#x4E2A;&#x7F51;&#x5740;&#x7684;&#x7AEF;&#x53E3;&#x4E0D;&#x540C;&#xFF0C;&#x8FD8;&#x662F;&#x8FBE;&#x4E0D;&#x5230;&#x540C;&#x6E90;&#x7684;&#x76EE;&#x7684;&#x3002;</p>
<p>&#x73B0;&#x5728;&#xFF0C;A &#x7F51;&#x9875;&#x901A;&#x8FC7;&#x811A;&#x672C;&#x8BBE;&#x7F6E;&#x4E00;&#x4E2A; Cookie&#x3002;</p>
<pre><code class="lang-javascript"><span class="hljs-built_in">document</span>.cookie = <span class="hljs-string">&quot;test1=hello&quot;</span>;
</code></pre>
<p>B &#x7F51;&#x9875;&#x5C31;&#x53EF;&#x4EE5;&#x8BFB;&#x5230;&#x8FD9;&#x4E2A; Cookie&#x3002;</p>
<pre><code class="lang-javascript"><span class="hljs-keyword">var</span> allCookie = <span class="hljs-built_in">document</span>.cookie;
</code></pre>
<p>&#x6CE8;&#x610F;&#xFF0C;&#x8FD9;&#x79CD;&#x65B9;&#x6CD5;&#x53EA;&#x9002;&#x7528;&#x4E8E; Cookie &#x548C; iframe &#x7A97;&#x53E3;&#xFF0C;LocalStorage &#x548C; IndexedDB &#x65E0;&#x6CD5;&#x901A;&#x8FC7;&#x8FD9;&#x79CD;&#x65B9;&#x6CD5;&#xFF0C;&#x89C4;&#x907F;&#x540C;&#x6E90;&#x653F;&#x7B56;&#xFF0C;&#x800C;&#x8981;&#x4F7F;&#x7528;&#x4E0B;&#x6587;&#x4ECB;&#x7ECD; PostMessage API&#x3002;</p>
<p>&#x53E6;&#x5916;&#xFF0C;&#x670D;&#x52A1;&#x5668;&#x4E5F;&#x53EF;&#x4EE5;&#x5728;&#x8BBE;&#x7F6E; Cookie &#x7684;&#x65F6;&#x5019;&#xFF0C;&#x6307;&#x5B9A; Cookie &#x7684;&#x6240;&#x5C5E;&#x57DF;&#x540D;&#x4E3A;&#x4E00;&#x7EA7;&#x57DF;&#x540D;&#xFF0C;&#x6BD4;&#x5982;<code>.example.com</code>&#x3002;</p>
<pre><code class="lang-http"><span class="hljs-attribute">Set-Cookie</span>: key=value; domain=.example.com; path=/
</code></pre>
<p>&#x8FD9;&#x6837;&#x7684;&#x8BDD;&#xFF0C;&#x4E8C;&#x7EA7;&#x57DF;&#x540D;&#x548C;&#x4E09;&#x7EA7;&#x57DF;&#x540D;&#x4E0D;&#x7528;&#x505A;&#x4EFB;&#x4F55;&#x8BBE;&#x7F6E;&#xFF0C;&#x90FD;&#x53EF;&#x4EE5;&#x8BFB;&#x53D6;&#x8FD9;&#x4E2A; Cookie&#x3002;</p>
<h2 id="iframe-&#x548C;&#x591A;&#x7A97;&#x53E3;&#x901A;&#x4FE1;">iframe &#x548C;&#x591A;&#x7A97;&#x53E3;&#x901A;&#x4FE1;</h2>
<p><code>iframe</code>&#x5143;&#x7D20;&#x53EF;&#x4EE5;&#x5728;&#x5F53;&#x524D;&#x7F51;&#x9875;&#x4E4B;&#x4E2D;&#xFF0C;&#x5D4C;&#x5165;&#x5176;&#x4ED6;&#x7F51;&#x9875;&#x3002;&#x6BCF;&#x4E2A;<code>iframe</code>&#x5143;&#x7D20;&#x5F62;&#x6210;&#x81EA;&#x5DF1;&#x7684;&#x7A97;&#x53E3;&#xFF0C;&#x5373;&#x6709;&#x81EA;&#x5DF1;&#x7684;<code>window</code>&#x5BF9;&#x8C61;&#x3002;<code>iframe</code>&#x7A97;&#x53E3;&#x4E4B;&#x4E2D;&#x7684;&#x811A;&#x672C;&#xFF0C;&#x53EF;&#x4EE5;&#x83B7;&#x5F97;&#x7236;&#x7A97;&#x53E3;&#x548C;&#x5B50;&#x7A97;&#x53E3;&#x3002;&#x4F46;&#x662F;&#xFF0C;&#x53EA;&#x6709;&#x5728;&#x540C;&#x6E90;&#x7684;&#x60C5;&#x51B5;&#x4E0B;&#xFF0C;&#x7236;&#x7A97;&#x53E3;&#x548C;&#x5B50;&#x7A97;&#x53E3;&#x624D;&#x80FD;&#x901A;&#x4FE1;&#xFF1B;&#x5982;&#x679C;&#x8DE8;&#x57DF;&#xFF0C;&#x5C31;&#x65E0;&#x6CD5;&#x62FF;&#x5230;&#x5BF9;&#x65B9;&#x7684; DOM&#x3002;</p>
<p>&#x6BD4;&#x5982;&#xFF0C;&#x7236;&#x7A97;&#x53E3;&#x8FD0;&#x884C;&#x4E0B;&#x9762;&#x7684;&#x547D;&#x4EE4;&#xFF0C;&#x5982;&#x679C;<code>iframe</code>&#x7A97;&#x53E3;&#x4E0D;&#x662F;&#x540C;&#x6E90;&#xFF0C;&#x5C31;&#x4F1A;&#x62A5;&#x9519;&#x3002;</p>
<pre><code class="lang-javascript"><span class="hljs-built_in">document</span>
.getElementById(<span class="hljs-string">&quot;myIFrame&quot;</span>)
.contentWindow
.document
<span class="hljs-comment">// Uncaught DOMException: Blocked a frame from accessing a cross-origin frame.</span>
</code></pre>
<p>&#x4E0A;&#x9762;&#x547D;&#x4EE4;&#x4E2D;&#xFF0C;&#x7236;&#x7A97;&#x53E3;&#x60F3;&#x83B7;&#x53D6;&#x5B50;&#x7A97;&#x53E3;&#x7684; DOM&#xFF0C;&#x56E0;&#x4E3A;&#x8DE8;&#x57DF;&#x5BFC;&#x81F4;&#x62A5;&#x9519;&#x3002;</p>
<p>&#x53CD;&#x4E4B;&#x4EA6;&#x7136;&#xFF0C;&#x5B50;&#x7A97;&#x53E3;&#x83B7;&#x53D6;&#x4E3B;&#x7A97;&#x53E3;&#x7684; DOM &#x4E5F;&#x4F1A;&#x62A5;&#x9519;&#x3002;</p>
<pre><code class="lang-javascript"><span class="hljs-built_in">window</span>.parent.document.body
<span class="hljs-comment">// &#x62A5;&#x9519;</span>
</code></pre>
<p>&#x8FD9;&#x79CD;&#x60C5;&#x51B5;&#x4E0D;&#x4EC5;&#x9002;&#x7528;&#x4E8E;<code>iframe</code>&#x7A97;&#x53E3;&#xFF0C;&#x8FD8;&#x9002;&#x7528;&#x4E8E;<code>window.open</code>&#x65B9;&#x6CD5;&#x6253;&#x5F00;&#x7684;&#x7A97;&#x53E3;&#xFF0C;&#x53EA;&#x8981;&#x8DE8;&#x57DF;&#xFF0C;&#x7236;&#x7A97;&#x53E3;&#x4E0E;&#x5B50;&#x7A97;&#x53E3;&#x4E4B;&#x95F4;&#x5C31;&#x65E0;&#x6CD5;&#x901A;&#x4FE1;&#x3002;</p>
<p>&#x5982;&#x679C;&#x4E24;&#x4E2A;&#x7A97;&#x53E3;&#x4E00;&#x7EA7;&#x57DF;&#x540D;&#x76F8;&#x540C;&#xFF0C;&#x53EA;&#x662F;&#x4E8C;&#x7EA7;&#x57DF;&#x540D;&#x4E0D;&#x540C;&#xFF0C;&#x90A3;&#x4E48;&#x8BBE;&#x7F6E;&#x4E0A;&#x4E00;&#x8282;&#x4ECB;&#x7ECD;&#x7684;<code>document.domain</code>&#x5C5E;&#x6027;&#xFF0C;&#x5C31;&#x53EF;&#x4EE5;&#x89C4;&#x907F;&#x540C;&#x6E90;&#x653F;&#x7B56;&#xFF0C;&#x62FF;&#x5230; DOM&#x3002;</p>
<p>&#x5BF9;&#x4E8E;&#x5B8C;&#x5168;&#x4E0D;&#x540C;&#x6E90;&#x7684;&#x7F51;&#x7AD9;&#xFF0C;&#x76EE;&#x524D;&#x6709;&#x4E24;&#x79CD;&#x65B9;&#x6CD5;&#xFF0C;&#x53EF;&#x4EE5;&#x89E3;&#x51B3;&#x8DE8;&#x57DF;&#x7A97;&#x53E3;&#x7684;&#x901A;&#x4FE1;&#x95EE;&#x9898;&#x3002;</p>
<blockquote>
<ul>
<li>&#x7247;&#x6BB5;&#x8BC6;&#x522B;&#x7B26;&#xFF08;fragment identifier&#xFF09;</li>
<li>&#x8DE8;&#x6587;&#x6863;&#x901A;&#x4FE1;API&#xFF08;Cross-document messaging&#xFF09;</li>
</ul>
</blockquote>
<h3 id="&#x7247;&#x6BB5;&#x8BC6;&#x522B;&#x7B26;">&#x7247;&#x6BB5;&#x8BC6;&#x522B;&#x7B26;</h3>
<p>&#x7247;&#x6BB5;&#x6807;&#x8BC6;&#x7B26;&#xFF08;fragment identifier&#xFF09;&#x6307;&#x7684;&#x662F;&#xFF0C;URL &#x7684;<code>#</code>&#x53F7;&#x540E;&#x9762;&#x7684;&#x90E8;&#x5206;&#xFF0C;&#x6BD4;&#x5982;<code>http://example.com/x.html#fragment</code>&#x7684;<code>#fragment</code>&#x3002;&#x5982;&#x679C;&#x53EA;&#x662F;&#x6539;&#x53D8;&#x7247;&#x6BB5;&#x6807;&#x8BC6;&#x7B26;&#xFF0C;&#x9875;&#x9762;&#x4E0D;&#x4F1A;&#x91CD;&#x65B0;&#x5237;&#x65B0;&#x3002;</p>
<p>&#x7236;&#x7A97;&#x53E3;&#x53EF;&#x4EE5;&#x628A;&#x4FE1;&#x606F;&#xFF0C;&#x5199;&#x5165;&#x5B50;&#x7A97;&#x53E3;&#x7684;&#x7247;&#x6BB5;&#x6807;&#x8BC6;&#x7B26;&#x3002;</p>
<pre><code class="lang-javascript"><span class="hljs-keyword">var</span> src = originURL + <span class="hljs-string">&apos;#&apos;</span> + data;
<span class="hljs-built_in">document</span>.getElementById(<span class="hljs-string">&apos;myIFrame&apos;</span>).src = src;
</code></pre>
<p>&#x4E0A;&#x9762;&#x4EE3;&#x7801;&#x4E2D;&#xFF0C;&#x7236;&#x7A97;&#x53E3;&#x628A;&#x6240;&#x8981;&#x4F20;&#x9012;&#x7684;&#x4FE1;&#x606F;&#xFF0C;&#x5199;&#x5165; iframe &#x7A97;&#x53E3;&#x7684;&#x7247;&#x6BB5;&#x6807;&#x8BC6;&#x7B26;&#x3002;</p>
<p>&#x5B50;&#x7A97;&#x53E3;&#x901A;&#x8FC7;&#x76D1;&#x542C;<code>hashchange</code>&#x4E8B;&#x4EF6;&#x5F97;&#x5230;&#x901A;&#x77E5;&#x3002;</p>
<pre><code class="lang-javascript"><span class="hljs-built_in">window</span>.onhashchange = checkMessage;

<span class="hljs-function"><span class="hljs-keyword">function</span> <span class="hljs-title">checkMessage</span>(<span class="hljs-params"></span>) </span>{
  <span class="hljs-keyword">var</span> message = <span class="hljs-built_in">window</span>.location.hash;
  <span class="hljs-comment">// ...</span>
}
</code></pre>
<p>&#x540C;&#x6837;&#x7684;&#xFF0C;&#x5B50;&#x7A97;&#x53E3;&#x4E5F;&#x53EF;&#x4EE5;&#x6539;&#x53D8;&#x7236;&#x7A97;&#x53E3;&#x7684;&#x7247;&#x6BB5;&#x6807;&#x8BC6;&#x7B26;&#x3002;</p>
<pre><code class="lang-javascript">parent.location.href = target + <span class="hljs-string">&apos;#&apos;</span> + hash;
</code></pre>
<h3 id="windowpostmessage">window.postMessage()</h3>
<p>&#x4E0A;&#x9762;&#x7684;&#x8FD9;&#x79CD;&#x65B9;&#x6CD5;&#x5C5E;&#x4E8E;&#x7834;&#x89E3;&#xFF0C;HTML5 &#x4E3A;&#x4E86;&#x89E3;&#x51B3;&#x8FD9;&#x4E2A;&#x95EE;&#x9898;&#xFF0C;&#x5F15;&#x5165;&#x4E86;&#x4E00;&#x4E2A;&#x5168;&#x65B0;&#x7684;API&#xFF1A;&#x8DE8;&#x6587;&#x6863;&#x901A;&#x4FE1; API&#xFF08;Cross-document messaging&#xFF09;&#x3002;</p>
<p>&#x8FD9;&#x4E2A; API &#x4E3A;<code>window</code>&#x5BF9;&#x8C61;&#x65B0;&#x589E;&#x4E86;&#x4E00;&#x4E2A;<code>window.postMessage</code>&#x65B9;&#x6CD5;&#xFF0C;&#x5141;&#x8BB8;&#x8DE8;&#x7A97;&#x53E3;&#x901A;&#x4FE1;&#xFF0C;&#x4E0D;&#x8BBA;&#x8FD9;&#x4E24;&#x4E2A;&#x7A97;&#x53E3;&#x662F;&#x5426;&#x540C;&#x6E90;&#x3002;&#x4E3E;&#x4F8B;&#x6765;&#x8BF4;&#xFF0C;&#x7236;&#x7A97;&#x53E3;<code>aaa.com</code>&#x5411;&#x5B50;&#x7A97;&#x53E3;<code>bbb.com</code>&#x53D1;&#x6D88;&#x606F;&#xFF0C;&#x8C03;&#x7528;<code>postMessage</code>&#x65B9;&#x6CD5;&#x5C31;&#x53EF;&#x4EE5;&#x4E86;&#x3002;</p>
<pre><code class="lang-javascript"><span class="hljs-comment">// &#x7236;&#x7A97;&#x53E3;&#x6253;&#x5F00;&#x4E00;&#x4E2A;&#x5B50;&#x7A97;&#x53E3;</span>
<span class="hljs-keyword">var</span> popup = <span class="hljs-built_in">window</span>.open(<span class="hljs-string">&apos;http://bbb.com&apos;</span>, <span class="hljs-string">&apos;title&apos;</span>);
<span class="hljs-comment">// &#x7236;&#x7A97;&#x53E3;&#x5411;&#x5B50;&#x7A97;&#x53E3;&#x53D1;&#x6D88;&#x606F;</span>
popup.postMessage(<span class="hljs-string">&apos;Hello World!&apos;</span>, <span class="hljs-string">&apos;http://bbb.com&apos;</span>);
</code></pre>
<p><code>postMessage</code>&#x65B9;&#x6CD5;&#x7684;&#x7B2C;&#x4E00;&#x4E2A;&#x53C2;&#x6570;&#x662F;&#x5177;&#x4F53;&#x7684;&#x4FE1;&#x606F;&#x5185;&#x5BB9;&#xFF0C;&#x7B2C;&#x4E8C;&#x4E2A;&#x53C2;&#x6570;&#x662F;&#x63A5;&#x6536;&#x6D88;&#x606F;&#x7684;&#x7A97;&#x53E3;&#x7684;&#x6E90;&#xFF08;origin&#xFF09;&#xFF0C;&#x5373;&#x201C;&#x534F;&#x8BAE; + &#x57DF;&#x540D; + &#x7AEF;&#x53E3;&#x201D;&#x3002;&#x4E5F;&#x53EF;&#x4EE5;&#x8BBE;&#x4E3A;<code>*</code>&#xFF0C;&#x8868;&#x793A;&#x4E0D;&#x9650;&#x5236;&#x57DF;&#x540D;&#xFF0C;&#x5411;&#x6240;&#x6709;&#x7A97;&#x53E3;&#x53D1;&#x9001;&#x3002;</p>
<p>&#x5B50;&#x7A97;&#x53E3;&#x5411;&#x7236;&#x7A97;&#x53E3;&#x53D1;&#x9001;&#x6D88;&#x606F;&#x7684;&#x5199;&#x6CD5;&#x7C7B;&#x4F3C;&#x3002;</p>
<pre><code class="lang-javascript"><span class="hljs-comment">// &#x5B50;&#x7A97;&#x53E3;&#x5411;&#x7236;&#x7A97;&#x53E3;&#x53D1;&#x6D88;&#x606F;</span>
<span class="hljs-built_in">window</span>.opener.postMessage(<span class="hljs-string">&apos;Nice to see you&apos;</span>, <span class="hljs-string">&apos;http://aaa.com&apos;</span>);
</code></pre>
<p>&#x7236;&#x7A97;&#x53E3;&#x548C;&#x5B50;&#x7A97;&#x53E3;&#x90FD;&#x53EF;&#x4EE5;&#x901A;&#x8FC7;<code>message</code>&#x4E8B;&#x4EF6;&#xFF0C;&#x76D1;&#x542C;&#x5BF9;&#x65B9;&#x7684;&#x6D88;&#x606F;&#x3002;</p>
<pre><code class="lang-javascript"><span class="hljs-comment">// &#x7236;&#x7A97;&#x53E3;&#x548C;&#x5B50;&#x7A97;&#x53E3;&#x90FD;&#x53EF;&#x4EE5;&#x7528;&#x4E0B;&#x9762;&#x7684;&#x4EE3;&#x7801;&#xFF0C;</span>
<span class="hljs-comment">// &#x76D1;&#x542C; message &#x6D88;&#x606F;</span>
<span class="hljs-built_in">window</span>.addEventListener(<span class="hljs-string">&apos;message&apos;</span>, <span class="hljs-function"><span class="hljs-keyword">function</span> (<span class="hljs-params">e</span>) </span>{
  <span class="hljs-built_in">console</span>.log(e.data);
},<span class="hljs-literal">false</span>);
</code></pre>
<p><code>message</code>&#x4E8B;&#x4EF6;&#x7684;&#x53C2;&#x6570;&#x662F;&#x4E8B;&#x4EF6;&#x5BF9;&#x8C61;<code>event</code>&#xFF0C;&#x63D0;&#x4F9B;&#x4EE5;&#x4E0B;&#x4E09;&#x4E2A;&#x5C5E;&#x6027;&#x3002;</p>
<blockquote>
<ul>
<li><code>event.source</code>&#xFF1A;&#x53D1;&#x9001;&#x6D88;&#x606F;&#x7684;&#x7A97;&#x53E3;</li>
<li><code>event.origin</code>: &#x6D88;&#x606F;&#x53D1;&#x5411;&#x7684;&#x7F51;&#x5740;</li>
<li><code>event.data</code>: &#x6D88;&#x606F;&#x5185;&#x5BB9;</li>
</ul>
</blockquote>
<p>&#x4E0B;&#x9762;&#x7684;&#x4F8B;&#x5B50;&#x662F;&#xFF0C;&#x5B50;&#x7A97;&#x53E3;&#x901A;&#x8FC7;<code>event.source</code>&#x5C5E;&#x6027;&#x5F15;&#x7528;&#x7236;&#x7A97;&#x53E3;&#xFF0C;&#x7136;&#x540E;&#x53D1;&#x9001;&#x6D88;&#x606F;&#x3002;</p>
<pre><code class="lang-javascript"><span class="hljs-built_in">window</span>.addEventListener(<span class="hljs-string">&apos;message&apos;</span>, receiveMessage);
<span class="hljs-function"><span class="hljs-keyword">function</span> <span class="hljs-title">receiveMessage</span>(<span class="hljs-params">event</span>) </span>{
  event.source.postMessage(<span class="hljs-string">&apos;Nice to see you!&apos;</span>, <span class="hljs-string">&apos;*&apos;</span>);
}
</code></pre>
<p>&#x4E0A;&#x9762;&#x4EE3;&#x7801;&#x6709;&#x51E0;&#x4E2A;&#x5730;&#x65B9;&#x9700;&#x8981;&#x6CE8;&#x610F;&#x3002;&#x9996;&#x5148;&#xFF0C;<code>receiveMessage</code>&#x51FD;&#x6570;&#x91CC;&#x9762;&#x6CA1;&#x6709;&#x8FC7;&#x6EE4;&#x4FE1;&#x606F;&#x7684;&#x6765;&#x6E90;&#xFF0C;&#x4EFB;&#x610F;&#x7F51;&#x5740;&#x53D1;&#x6765;&#x7684;&#x4FE1;&#x606F;&#x90FD;&#x4F1A;&#x88AB;&#x5904;&#x7406;&#x3002;&#x5176;&#x6B21;&#xFF0C;<code>postMessage</code>&#x65B9;&#x6CD5;&#x4E2D;&#x6307;&#x5B9A;&#x7684;&#x76EE;&#x6807;&#x7A97;&#x53E3;&#x7684;&#x7F51;&#x5740;&#x662F;&#x4E00;&#x4E2A;&#x661F;&#x53F7;&#xFF0C;&#x8868;&#x793A;&#x8BE5;&#x4FE1;&#x606F;&#x53EF;&#x4EE5;&#x5411;&#x4EFB;&#x610F;&#x7F51;&#x5740;&#x53D1;&#x9001;&#x3002;&#x901A;&#x5E38;&#x6765;&#x8BF4;&#xFF0C;&#x8FD9;&#x4E24;&#x79CD;&#x505A;&#x6CD5;&#x662F;&#x4E0D;&#x63A8;&#x8350;&#x7684;&#xFF0C;&#x56E0;&#x4E3A;&#x4E0D;&#x591F;&#x5B89;&#x5168;&#xFF0C;&#x53EF;&#x80FD;&#x4F1A;&#x88AB;&#x6076;&#x610F;&#x5229;&#x7528;&#x3002;</p>
<p><code>event.origin</code>&#x5C5E;&#x6027;&#x53EF;&#x4EE5;&#x8FC7;&#x6EE4;&#x4E0D;&#x662F;&#x53D1;&#x7ED9;&#x672C;&#x7A97;&#x53E3;&#x7684;&#x6D88;&#x606F;&#x3002;</p>
<pre><code class="lang-javascript"><span class="hljs-built_in">window</span>.addEventListener(<span class="hljs-string">&apos;message&apos;</span>, receiveMessage);
<span class="hljs-function"><span class="hljs-keyword">function</span> <span class="hljs-title">receiveMessage</span>(<span class="hljs-params">event</span>) </span>{
  <span class="hljs-keyword">if</span> (event.origin !== <span class="hljs-string">&apos;http://aaa.com&apos;</span>) <span class="hljs-keyword">return</span>;
  <span class="hljs-keyword">if</span> (event.data === <span class="hljs-string">&apos;Hello World&apos;</span>) {
    event.source.postMessage(<span class="hljs-string">&apos;Hello&apos;</span>, event.origin);
  } <span class="hljs-keyword">else</span> {
    <span class="hljs-built_in">console</span>.log(event.data);
  }
}
</code></pre>
<h3 id="localstorage">LocalStorage</h3>
<p>&#x901A;&#x8FC7;<code>window.postMessage</code>&#xFF0C;&#x8BFB;&#x5199;&#x5176;&#x4ED6;&#x7A97;&#x53E3;&#x7684; LocalStorage &#x4E5F;&#x6210;&#x4E3A;&#x4E86;&#x53EF;&#x80FD;&#x3002;</p>
<p>&#x4E0B;&#x9762;&#x662F;&#x4E00;&#x4E2A;&#x4F8B;&#x5B50;&#xFF0C;&#x4E3B;&#x7A97;&#x53E3;&#x5199;&#x5165; iframe &#x5B50;&#x7A97;&#x53E3;&#x7684;<code>localStorage</code>&#x3002;</p>
<pre><code class="lang-javascript"><span class="hljs-built_in">window</span>.onmessage = <span class="hljs-function"><span class="hljs-keyword">function</span>(<span class="hljs-params">e</span>) </span>{
  <span class="hljs-keyword">if</span> (e.origin !== <span class="hljs-string">&apos;http://bbb.com&apos;</span>) {
    <span class="hljs-keyword">return</span>;
  }
  <span class="hljs-keyword">var</span> payload = <span class="hljs-built_in">JSON</span>.parse(e.data);
  localStorage.setItem(payload.key, <span class="hljs-built_in">JSON</span>.stringify(payload.data));
};
</code></pre>
<p>&#x4E0A;&#x9762;&#x4EE3;&#x7801;&#x4E2D;&#xFF0C;&#x5B50;&#x7A97;&#x53E3;&#x5C06;&#x7236;&#x7A97;&#x53E3;&#x53D1;&#x6765;&#x7684;&#x6D88;&#x606F;&#xFF0C;&#x5199;&#x5165;&#x81EA;&#x5DF1;&#x7684; LocalStorage&#x3002;</p>
<p>&#x7236;&#x7A97;&#x53E3;&#x53D1;&#x9001;&#x6D88;&#x606F;&#x7684;&#x4EE3;&#x7801;&#x5982;&#x4E0B;&#x3002;</p>
<pre><code class="lang-javascript"><span class="hljs-keyword">var</span> win = <span class="hljs-built_in">document</span>.getElementsByTagName(<span class="hljs-string">&apos;iframe&apos;</span>)[<span class="hljs-number">0</span>].contentWindow;
<span class="hljs-keyword">var</span> obj = { name: <span class="hljs-string">&apos;Jack&apos;</span> };
win.postMessage(
  <span class="hljs-built_in">JSON</span>.stringify({key: <span class="hljs-string">&apos;storage&apos;</span>, data: obj}),
  <span class="hljs-string">&apos;http://bbb.com&apos;</span>
);
</code></pre>
<p>&#x52A0;&#x5F3A;&#x7248;&#x7684;&#x5B50;&#x7A97;&#x53E3;&#x63A5;&#x6536;&#x6D88;&#x606F;&#x7684;&#x4EE3;&#x7801;&#x5982;&#x4E0B;&#x3002;</p>
<pre><code class="lang-javascript"><span class="hljs-built_in">window</span>.onmessage = <span class="hljs-function"><span class="hljs-keyword">function</span>(<span class="hljs-params">e</span>) </span>{
  <span class="hljs-keyword">if</span> (e.origin !== <span class="hljs-string">&apos;http://bbb.com&apos;</span>) <span class="hljs-keyword">return</span>;
  <span class="hljs-keyword">var</span> payload = <span class="hljs-built_in">JSON</span>.parse(e.data);
  <span class="hljs-keyword">switch</span> (payload.method) {
    <span class="hljs-keyword">case</span> <span class="hljs-string">&apos;set&apos;</span>:
      localStorage.setItem(payload.key, <span class="hljs-built_in">JSON</span>.stringify(payload.data));
      <span class="hljs-keyword">break</span>;
    <span class="hljs-keyword">case</span> <span class="hljs-string">&apos;get&apos;</span>:
      <span class="hljs-keyword">var</span> parent = <span class="hljs-built_in">window</span>.parent;
      <span class="hljs-keyword">var</span> data = localStorage.getItem(payload.key);
      parent.postMessage(data, <span class="hljs-string">&apos;http://aaa.com&apos;</span>);
      <span class="hljs-keyword">break</span>;
    <span class="hljs-keyword">case</span> <span class="hljs-string">&apos;remove&apos;</span>:
      localStorage.removeItem(payload.key);
      <span class="hljs-keyword">break</span>;
  }
};
</code></pre>
<p>&#x52A0;&#x5F3A;&#x7248;&#x7684;&#x7236;&#x7A97;&#x53E3;&#x53D1;&#x9001;&#x6D88;&#x606F;&#x4EE3;&#x7801;&#x5982;&#x4E0B;&#x3002;</p>
<pre><code class="lang-javascript"><span class="hljs-keyword">var</span> win = <span class="hljs-built_in">document</span>.getElementsByTagName(<span class="hljs-string">&apos;iframe&apos;</span>)[<span class="hljs-number">0</span>].contentWindow;
<span class="hljs-keyword">var</span> obj = { name: <span class="hljs-string">&apos;Jack&apos;</span> };
<span class="hljs-comment">// &#x5B58;&#x5165;&#x5BF9;&#x8C61;</span>
win.postMessage(
  <span class="hljs-built_in">JSON</span>.stringify({key: <span class="hljs-string">&apos;storage&apos;</span>, method: <span class="hljs-string">&apos;set&apos;</span>, data: obj}),
  <span class="hljs-string">&apos;http://bbb.com&apos;</span>
);
<span class="hljs-comment">// &#x8BFB;&#x53D6;&#x5BF9;&#x8C61;</span>
win.postMessage(
  <span class="hljs-built_in">JSON</span>.stringify({key: <span class="hljs-string">&apos;storage&apos;</span>, method: <span class="hljs-string">&quot;get&quot;</span>}),
  <span class="hljs-string">&quot;*&quot;</span>
);
<span class="hljs-built_in">window</span>.onmessage = <span class="hljs-function"><span class="hljs-keyword">function</span>(<span class="hljs-params">e</span>) </span>{
  <span class="hljs-keyword">if</span> (e.origin != <span class="hljs-string">&apos;http://aaa.com&apos;</span>) <span class="hljs-keyword">return</span>;
  <span class="hljs-built_in">console</span>.log(<span class="hljs-built_in">JSON</span>.parse(e.data).name);
};
</code></pre>
<h2 id="ajax">AJAX</h2>
<p>&#x540C;&#x6E90;&#x653F;&#x7B56;&#x89C4;&#x5B9A;&#xFF0C;AJAX &#x8BF7;&#x6C42;&#x53EA;&#x80FD;&#x53D1;&#x7ED9;&#x540C;&#x6E90;&#x7684;&#x7F51;&#x5740;&#xFF0C;&#x5426;&#x5219;&#x5C31;&#x62A5;&#x9519;&#x3002;</p>
<p>&#x9664;&#x4E86;&#x67B6;&#x8BBE;&#x670D;&#x52A1;&#x5668;&#x4EE3;&#x7406;&#xFF08;&#x6D4F;&#x89C8;&#x5668;&#x8BF7;&#x6C42;&#x540C;&#x6E90;&#x670D;&#x52A1;&#x5668;&#xFF0C;&#x518D;&#x7531;&#x540E;&#x8005;&#x8BF7;&#x6C42;&#x5916;&#x90E8;&#x670D;&#x52A1;&#xFF09;&#xFF0C;&#x6709;&#x4E09;&#x79CD;&#x65B9;&#x6CD5;&#x89C4;&#x907F;&#x8FD9;&#x4E2A;&#x9650;&#x5236;&#x3002;</p>
<blockquote>
<ul>
<li>JSONP</li>
<li>WebSocket</li>
<li>CORS</li>
</ul>
</blockquote>
<h3 id="jsonp">JSONP</h3>
<p>JSONP &#x662F;&#x670D;&#x52A1;&#x5668;&#x4E0E;&#x5BA2;&#x6237;&#x7AEF;&#x8DE8;&#x6E90;&#x901A;&#x4FE1;&#x7684;&#x5E38;&#x7528;&#x65B9;&#x6CD5;&#x3002;&#x6700;&#x5927;&#x7279;&#x70B9;&#x5C31;&#x662F;&#x7B80;&#x5355;&#x6613;&#x7528;&#xFF0C;&#x6CA1;&#x6709;&#x517C;&#x5BB9;&#x6027;&#x95EE;&#x9898;&#xFF0C;&#x8001;&#x5F0F;&#x6D4F;&#x89C8;&#x5668;&#x5168;&#x90E8;&#x652F;&#x6301;&#xFF0C;&#x670D;&#x52A1;&#x7AEF;&#x6539;&#x9020;&#x975E;&#x5E38;&#x5C0F;&#x3002;</p>
<p>&#x5B83;&#x7684;&#x505A;&#x6CD5;&#x5982;&#x4E0B;&#x3002;</p>
<p>&#x7B2C;&#x4E00;&#x6B65;&#xFF0C;&#x7F51;&#x9875;&#x6DFB;&#x52A0;&#x4E00;&#x4E2A;<code>&lt;script&gt;</code>&#x5143;&#x7D20;&#xFF0C;&#x5411;&#x670D;&#x52A1;&#x5668;&#x8BF7;&#x6C42;&#x4E00;&#x4E2A;&#x811A;&#x672C;&#xFF0C;&#x8FD9;&#x4E0D;&#x53D7;&#x540C;&#x6E90;&#x653F;&#x7B56;&#x9650;&#x5236;&#xFF0C;&#x53EF;&#x4EE5;&#x8DE8;&#x57DF;&#x8BF7;&#x6C42;&#x3002;</p>
<pre><code class="lang-html"><span class="hljs-tag">&lt;<span class="hljs-name">script</span> <span class="hljs-attr">src</span>=<span class="hljs-string">&quot;http://api.foo.com?callback=bar&quot;</span>&gt;</span><span class="undefined"></span><span class="hljs-tag">&lt;/<span class="hljs-name">script</span>&gt;</span>
</code></pre>
<p>&#x6CE8;&#x610F;&#xFF0C;&#x8BF7;&#x6C42;&#x7684;&#x811A;&#x672C;&#x7F51;&#x5740;&#x6709;&#x4E00;&#x4E2A;<code>callback</code>&#x53C2;&#x6570;&#xFF08;<code>?callback=bar</code>&#xFF09;&#xFF0C;&#x7528;&#x6765;&#x544A;&#x8BC9;&#x670D;&#x52A1;&#x5668;&#xFF0C;&#x5BA2;&#x6237;&#x7AEF;&#x7684;&#x56DE;&#x8C03;&#x51FD;&#x6570;&#x540D;&#x79F0;&#xFF08;<code>bar</code>&#xFF09;&#x3002;</p>
<p>&#x7B2C;&#x4E8C;&#x6B65;&#xFF0C;&#x670D;&#x52A1;&#x5668;&#x6536;&#x5230;&#x8BF7;&#x6C42;&#x540E;&#xFF0C;&#x62FC;&#x63A5;&#x4E00;&#x4E2A;&#x5B57;&#x7B26;&#x4E32;&#xFF0C;&#x5C06; JSON &#x6570;&#x636E;&#x653E;&#x5728;&#x51FD;&#x6570;&#x540D;&#x91CC;&#x9762;&#xFF0C;&#x4F5C;&#x4E3A;&#x5B57;&#x7B26;&#x4E32;&#x8FD4;&#x56DE;&#xFF08;<code>bar({...})</code>&#xFF09;&#x3002;</p>
<p>&#x7B2C;&#x4E09;&#x6B65;&#xFF0C;&#x5BA2;&#x6237;&#x7AEF;&#x4F1A;&#x5C06;&#x670D;&#x52A1;&#x5668;&#x8FD4;&#x56DE;&#x7684;&#x5B57;&#x7B26;&#x4E32;&#xFF0C;&#x4F5C;&#x4E3A;&#x4EE3;&#x7801;&#x89E3;&#x6790;&#xFF0C;&#x56E0;&#x4E3A;&#x6D4F;&#x89C8;&#x5668;&#x8BA4;&#x4E3A;&#xFF0C;&#x8FD9;&#x662F;<code>&lt;script&gt;</code>&#x6807;&#x7B7E;&#x8BF7;&#x6C42;&#x7684;&#x811A;&#x672C;&#x5185;&#x5BB9;&#x3002;&#x8FD9;&#x65F6;&#xFF0C;&#x5BA2;&#x6237;&#x7AEF;&#x53EA;&#x8981;&#x5B9A;&#x4E49;&#x4E86;<code>bar()</code>&#x51FD;&#x6570;&#xFF0C;&#x5C31;&#x80FD;&#x5728;&#x8BE5;&#x51FD;&#x6570;&#x4F53;&#x5185;&#xFF0C;&#x62FF;&#x5230;&#x670D;&#x52A1;&#x5668;&#x8FD4;&#x56DE;&#x7684; JSON &#x6570;&#x636E;&#x3002;</p>
<p>&#x4E0B;&#x9762;&#x770B;&#x4E00;&#x4E2A;&#x5B9E;&#x4F8B;&#x3002;&#x9996;&#x5148;&#xFF0C;&#x7F51;&#x9875;&#x52A8;&#x6001;&#x63D2;&#x5165;<code>&lt;script&gt;</code>&#x5143;&#x7D20;&#xFF0C;&#x7531;&#x5B83;&#x5411;&#x8DE8;&#x57DF;&#x7F51;&#x5740;&#x53D1;&#x51FA;&#x8BF7;&#x6C42;&#x3002;</p>
<pre><code class="lang-javascript"><span class="hljs-function"><span class="hljs-keyword">function</span> <span class="hljs-title">addScriptTag</span>(<span class="hljs-params">src</span>) </span>{
  <span class="hljs-keyword">var</span> script = <span class="hljs-built_in">document</span>.createElement(<span class="hljs-string">&apos;script&apos;</span>);
  script.setAttribute(<span class="hljs-string">&apos;type&apos;</span>, <span class="hljs-string">&apos;text/javascript&apos;</span>);
  script.src = src;
  <span class="hljs-built_in">document</span>.body.appendChild(script);
}

<span class="hljs-built_in">window</span>.onload = <span class="hljs-function"><span class="hljs-keyword">function</span> (<span class="hljs-params"></span>) </span>{
  addScriptTag(<span class="hljs-string">&apos;http://example.com/ip?callback=foo&apos;</span>);
}

<span class="hljs-function"><span class="hljs-keyword">function</span> <span class="hljs-title">foo</span>(<span class="hljs-params">data</span>) </span>{
  <span class="hljs-built_in">console</span>.log(<span class="hljs-string">&apos;Your public IP address is: &apos;</span> + data.ip);
};
</code></pre>
<p>&#x4E0A;&#x9762;&#x4EE3;&#x7801;&#x901A;&#x8FC7;&#x52A8;&#x6001;&#x6DFB;&#x52A0;<code>&lt;script&gt;</code>&#x5143;&#x7D20;&#xFF0C;&#x5411;&#x670D;&#x52A1;&#x5668;<code>example.com</code>&#x53D1;&#x51FA;&#x8BF7;&#x6C42;&#x3002;&#x6CE8;&#x610F;&#xFF0C;&#x8BE5;&#x8BF7;&#x6C42;&#x7684;&#x67E5;&#x8BE2;&#x5B57;&#x7B26;&#x4E32;&#x6709;&#x4E00;&#x4E2A;<code>callback</code>&#x53C2;&#x6570;&#xFF0C;&#x7528;&#x6765;&#x6307;&#x5B9A;&#x56DE;&#x8C03;&#x51FD;&#x6570;&#x7684;&#x540D;&#x5B57;&#xFF0C;&#x8FD9;&#x5BF9;&#x4E8E; JSONP &#x662F;&#x5FC5;&#x9700;&#x7684;&#x3002;</p>
<p>&#x670D;&#x52A1;&#x5668;&#x6536;&#x5230;&#x8FD9;&#x4E2A;&#x8BF7;&#x6C42;&#x4EE5;&#x540E;&#xFF0C;&#x4F1A;&#x5C06;&#x6570;&#x636E;&#x653E;&#x5728;&#x56DE;&#x8C03;&#x51FD;&#x6570;&#x7684;&#x53C2;&#x6570;&#x4F4D;&#x7F6E;&#x8FD4;&#x56DE;&#x3002;</p>
<pre><code class="lang-javascript">foo({
  <span class="hljs-string">&apos;ip&apos;</span>: <span class="hljs-string">&apos;8.8.8.8&apos;</span>
});
</code></pre>
<p>&#x7531;&#x4E8E;<code>&lt;script&gt;</code>&#x5143;&#x7D20;&#x8BF7;&#x6C42;&#x7684;&#x811A;&#x672C;&#xFF0C;&#x76F4;&#x63A5;&#x4F5C;&#x4E3A;&#x4EE3;&#x7801;&#x8FD0;&#x884C;&#x3002;&#x8FD9;&#x65F6;&#xFF0C;&#x53EA;&#x8981;&#x6D4F;&#x89C8;&#x5668;&#x5B9A;&#x4E49;&#x4E86;<code>foo</code>&#x51FD;&#x6570;&#xFF0C;&#x8BE5;&#x51FD;&#x6570;&#x5C31;&#x4F1A;&#x7ACB;&#x5373;&#x8C03;&#x7528;&#x3002;&#x4F5C;&#x4E3A;&#x53C2;&#x6570;&#x7684; JSON &#x6570;&#x636E;&#x88AB;&#x89C6;&#x4E3A; JavaScript &#x5BF9;&#x8C61;&#xFF0C;&#x800C;&#x4E0D;&#x662F;&#x5B57;&#x7B26;&#x4E32;&#xFF0C;&#x56E0;&#x6B64;&#x907F;&#x514D;&#x4E86;&#x4F7F;&#x7528;<code>JSON.parse</code>&#x7684;&#x6B65;&#x9AA4;&#x3002;</p>
<h3 id="websocket">WebSocket</h3>
<p>WebSocket &#x662F;&#x4E00;&#x79CD;&#x901A;&#x4FE1;&#x534F;&#x8BAE;&#xFF0C;&#x4F7F;&#x7528;<code>ws://</code>&#xFF08;&#x975E;&#x52A0;&#x5BC6;&#xFF09;&#x548C;<code>wss://</code>&#xFF08;&#x52A0;&#x5BC6;&#xFF09;&#x4F5C;&#x4E3A;&#x534F;&#x8BAE;&#x524D;&#x7F00;&#x3002;&#x8BE5;&#x534F;&#x8BAE;&#x4E0D;&#x5B9E;&#x884C;&#x540C;&#x6E90;&#x653F;&#x7B56;&#xFF0C;&#x53EA;&#x8981;&#x670D;&#x52A1;&#x5668;&#x652F;&#x6301;&#xFF0C;&#x5C31;&#x53EF;&#x4EE5;&#x901A;&#x8FC7;&#x5B83;&#x8FDB;&#x884C;&#x8DE8;&#x6E90;&#x901A;&#x4FE1;&#x3002;</p>
<p>&#x4E0B;&#x9762;&#x662F;&#x4E00;&#x4E2A;&#x4F8B;&#x5B50;&#xFF0C;&#x6D4F;&#x89C8;&#x5668;&#x53D1;&#x51FA;&#x7684; WebSocket &#x8BF7;&#x6C42;&#x7684;&#x5934;&#x4FE1;&#x606F;&#xFF08;&#x6458;&#x81EA;<a href="https://en.wikipedia.org/wiki/WebSocket" target="_blank" rel="nofollow">&#x7EF4;&#x57FA;&#x767E;&#x79D1;</a>&#xFF09;&#x3002;</p>
<pre><code class="lang-http"><span class="hljs-keyword">GET</span> <span class="hljs-string">/chat</span> HTTP/1.1
<span class="hljs-attribute">Host</span>: server.example.com
<span class="hljs-attribute">Upgrade</span>: websocket
<span class="hljs-attribute">Connection</span>: Upgrade
<span class="hljs-attribute">Sec-WebSocket-Key</span>: x3JJHMbDL1EzLkh9GBhXDw==
<span class="hljs-attribute">Sec-WebSocket-Protocol</span>: chat, superchat
<span class="hljs-attribute">Sec-WebSocket-Version</span>: 13
<span class="hljs-attribute">Origin</span>: http://example.com
</code></pre>
<p>&#x4E0A;&#x9762;&#x4EE3;&#x7801;&#x4E2D;&#xFF0C;&#x6709;&#x4E00;&#x4E2A;&#x5B57;&#x6BB5;&#x662F;<code>Origin</code>&#xFF0C;&#x8868;&#x793A;&#x8BE5;&#x8BF7;&#x6C42;&#x7684;&#x8BF7;&#x6C42;&#x6E90;&#xFF08;origin&#xFF09;&#xFF0C;&#x5373;&#x53D1;&#x81EA;&#x54EA;&#x4E2A;&#x57DF;&#x540D;&#x3002;</p>
<p>&#x6B63;&#x662F;&#x56E0;&#x4E3A;&#x6709;&#x4E86;<code>Origin</code>&#x8FD9;&#x4E2A;&#x5B57;&#x6BB5;&#xFF0C;&#x6240;&#x4EE5; WebSocket &#x624D;&#x6CA1;&#x6709;&#x5B9E;&#x884C;&#x540C;&#x6E90;&#x653F;&#x7B56;&#x3002;&#x56E0;&#x4E3A;&#x670D;&#x52A1;&#x5668;&#x53EF;&#x4EE5;&#x6839;&#x636E;&#x8FD9;&#x4E2A;&#x5B57;&#x6BB5;&#xFF0C;&#x5224;&#x65AD;&#x662F;&#x5426;&#x8BB8;&#x53EF;&#x672C;&#x6B21;&#x901A;&#x4FE1;&#x3002;&#x5982;&#x679C;&#x8BE5;&#x57DF;&#x540D;&#x5728;&#x767D;&#x540D;&#x5355;&#x5185;&#xFF0C;&#x670D;&#x52A1;&#x5668;&#x5C31;&#x4F1A;&#x505A;&#x51FA;&#x5982;&#x4E0B;&#x56DE;&#x5E94;&#x3002;</p>
<pre><code class="lang-http">HTTP/1.1 <span class="hljs-number">101</span> Switching Protocols
<span class="hljs-attribute">Upgrade</span>: websocket
<span class="hljs-attribute">Connection</span>: Upgrade
<span class="hljs-attribute">Sec-WebSocket-Accept</span>: HSmrc0sMlYUkAGmm5OPpG2HaGWk=
<span class="hljs-attribute">Sec-WebSocket-Protocol</span>: chat
</code></pre>
<h3 id="cors">CORS</h3>
<p>CORS &#x662F;&#x8DE8;&#x6E90;&#x8D44;&#x6E90;&#x5206;&#x4EAB;&#xFF08;Cross-Origin Resource Sharing&#xFF09;&#x7684;&#x7F29;&#x5199;&#x3002;&#x5B83;&#x662F; W3C &#x6807;&#x51C6;&#xFF0C;&#x5C5E;&#x4E8E;&#x8DE8;&#x6E90; AJAX &#x8BF7;&#x6C42;&#x7684;&#x6839;&#x672C;&#x89E3;&#x51B3;&#x65B9;&#x6CD5;&#x3002;&#x76F8;&#x6BD4; JSONP &#x53EA;&#x80FD;&#x53D1;<code>GET</code>&#x8BF7;&#x6C42;&#xFF0C;CORS &#x5141;&#x8BB8;&#x4EFB;&#x4F55;&#x7C7B;&#x578B;&#x7684;&#x8BF7;&#x6C42;&#x3002;</p>
<p>&#x4E0B;&#x4E00;&#x7AE0;&#x5C06;&#x8BE6;&#x7EC6;&#x4ECB;&#x7ECD;&#xFF0C;&#x5982;&#x4F55;&#x901A;&#x8FC7; CORS &#x5B8C;&#x6210;&#x8DE8;&#x6E90; AJAX &#x8BF7;&#x6C42;&#x3002;</p>
<h2 id="&#x53C2;&#x8003;&#x94FE;&#x63A5;">&#x53C2;&#x8003;&#x94FE;&#x63A5;</h2>
<ul>
<li>Mozilla Developer Network, <a href="https://developer.mozilla.org/en-US/docs/Web/API/window.postMessage" target="_blank" rel="nofollow">Window.postMessage</a></li>
<li>Jakub Jankiewicz, <a href="http://jcubic.wordpress.com/2014/06/20/cross-domain-localstorage/" target="_blank" rel="nofollow">Cross-Domain LocalStorage</a></li>
<li>David Baron, <a href="http://dbaron.org/log/20100309-faster-timeouts" target="_blank" rel="nofollow">setTimeout with a shorter delay</a>: &#x5229;&#x7528; window.postMessage &#x53EF;&#x4EE5;&#x5B9E;&#x73B0;0&#x6BEB;&#x79D2;&#x89E6;&#x53D1;&#x56DE;&#x8C03;&#x51FD;&#x6570;</li>
</ul>




<footer class="page-footer-ex"> <span class="page-footer-ex-copyright"> <a href="https://xiaobaiai.net" target="_blank" rel="nofollow">&#x5C0F;&#x767D;AI.&#x6613;&#x540D;</a>, &#x4F7F;&#x7528;<a href="https://creativecommons.org/licenses/by-sa/4.0/" target="_blank" rel="nofollow">&#x77E5;&#x8BC6;&#x5171;&#x4EAB; 4.0 &#x534F;&#x8BAE;&#x53D1;&#x5E03;</a>&#x53D1;&#x5E03; </span> &#xA0;&#xA0;&#xA0;&#xA0;&#xA0;&#xA0;&#xA0;&#xA0;&#xA0;&#xA0; <span class="page-footer-ex-footer-update"> <i>&#x9875;&#x9762;&#x4FEE;&#x8BA2;&#x4E8E;&#xFF1A;</i> 2019-08-22 16:53:02 </span> </footer>
                                
                                </section>
                            
    </div>
    <div class="search-results">
        <div class="has-results">
            
            <h1 class="search-results-title"><span class='search-results-count'></span> results matching "<span class='search-query'></span>"</h1>
            <ul class="search-results-list"></ul>
            
        </div>
        <div class="no-results">
            
            <h1 class="search-results-title">No results matching "<span class='search-query'></span>"</h1>
            
        </div>
    </div>
</div>

    </div>
    <div class="search-results">
        <div class="has-results">
            
            <h1 class="search-results-title"><span class='search-results-count'></span> results matching "<span class='search-query'></span>"</h1>
            <ul class="search-results-list"></ul>
            
        </div>
        <div class="no-results">
            
            <h1 class="search-results-title">No results matching "<span class='search-query'></span>"</h1>
            
        </div>
    </div>
</div>

                        </div>
                    </div>
                
            </div>

            
                
                <a href="xmlhttprequest.html" class="navigation navigation-prev " aria-label="Previous page: XMLHttpRequest 对象">
                    <i class="fa fa-angle-left"></i>
                </a>
                
                
                <a href="cors.html" class="navigation navigation-next " aria-label="Next page: CORS 通信">
                    <i class="fa fa-angle-right"></i>
                </a>
                
            
        
    </div>

    <script>
        var gitbook = gitbook || [];
        gitbook.push(function() {
            gitbook.page.hasChanged({"page":{"title":"同源限制","level":"1.11.6","depth":2,"next":{"title":"CORS 通信","level":"1.11.7","depth":2,"path":"docs/bom/cors.md","ref":"docs/bom/cors.md","articles":[]},"previous":{"title":"XMLHttpRequest 对象","level":"1.11.5","depth":2,"path":"docs/bom/xmlhttprequest.md","ref":"docs/bom/xmlhttprequest.md","articles":[]},"dir":"ltr"},"config":{"plugins":["-sharing","chapter-fold","back-to-top-button","search-pro","sharing-plus","myseo","favicon","page-footer-ex","search-pro","livereload"],"styles":{"website":"styles/website.css","pdf":"styles/pdf.css","epub":"styles/epub.css","mobi":"styles/mobi.css","ebook":"styles/ebook.css","print":"styles/print.css"},"pluginsConfig":{"chapter-fold":{},"livereload":{},"page-footer-ex":{"copyright":"<a href='https://xiaobaiai.net' target='_blank'>小白AI.易名</a>, 使用<a href='https://creativecommons.org/licenses/by-sa/4.0/' target='_blank'>知识共享 4.0 协议发布</a>发布","markdown":false,"update_format":"YYYY-MM-DD HH:mm:ss","update_label":"<i>页面修订于：</i>"},"search-pro":{},"search":{},"sharing-plus":{"qq":false,"all":["facebook","google","twitter","instapaper","linkedin","pocket","stumbleupon"],"douban":false,"facebook":true,"weibo":false,"instapaper":false,"whatsapp":false,"hatenaBookmark":false,"twitter":true,"messenger":false,"line":false,"vk":false,"pocket":true,"google":false,"viber":false,"stumbleupon":false,"qzone":false,"linkedin":false},"lunr":{"maxIndexSize":1000000,"ignoreSpecialCharacters":false},"myseo":{"description":"Javascript教程","keywords":"小白AI.易名,xiaobaiai.net,Javascript,入门,教程,博客","titlePrefix":"Javascript教程 - ","titleSuffix":" - 小白AI.易名"},"fontsettings":{"theme":"white","family":"sans","size":2},"highlight":{},"favicon":{"shortcut":"assets/images/favicon.ico","bookmark":"assets/images/favicon.ico","appleTouch":"assets/images/apple-touch-icon.png","appleTouchMore":{"120x120":"assets/images/apple-touch-icon-120x120.png","180x180":"assets/images/apple-touch-icon-180x180.png"}},"back-to-top-button":{},"sharing":{"qq":true,"all":["qq","qzone","twitter","weibo","linkedin"],"douban":false,"facebook":true,"weibo":true,"instapaper":false,"whatsapp":false,"hatenaBookmark":false,"twitter":false,"messenger":false,"line":false,"vk":false,"pocket":false,"google":false,"viber":false,"stumbleupon":false,"qzone":true,"linkedin":false},"theme-default":{"styles":{"website":"styles/website.css","pdf":"styles/pdf.css","epub":"styles/epub.css","mobi":"styles/mobi.css","ebook":"styles/ebook.css","print":"styles/print.css"},"showLevel":false}},"theme":"default","author":"小白AI.易名","pdf":{"pageNumbers":true,"fontSize":12,"fontFamily":"Arial","paperSize":"a4","chapterMark":"pagebreak","pageBreaksBefore":"/","margin":{"right":62,"left":62,"top":56,"bottom":56}},"structure":{"langs":"LANGS.md","readme":"README.md","glossary":"GLOSSARY.md","summary":"SUMMARY.md"},"variables":{},"language":"zh-hans","gitbook":"*","description":"Javascript教程"},"file":{"path":"docs/bom/same-origin.md","mtime":"2019-08-22T08:53:02.303Z","type":"markdown"},"gitbook":{"version":"3.2.3","time":"2019-08-28T05:00:46.673Z"},"basePath":"../..","book":{"language":""}});
        });
    </script>
</div>

        
    <script src="../../gitbook/gitbook.js"></script>
    <script src="../../gitbook/theme.js"></script>
    
        
        <script src="../../gitbook/gitbook-plugin-chapter-fold/chapter-fold.js"></script>
        
    
        
        <script src="../../gitbook/gitbook-plugin-back-to-top-button/plugin.js"></script>
        
    
        
        <script src="../../gitbook/gitbook-plugin-search-pro/jquery.mark.min.js"></script>
        
    
        
        <script src="../../gitbook/gitbook-plugin-search-pro/search.js"></script>
        
    
        
        <script src="../../gitbook/gitbook-plugin-sharing-plus/buttons.js"></script>
        
    
        
        <script src="../../gitbook/gitbook-plugin-livereload/plugin.js"></script>
        
    
        
        <script src="../../gitbook/gitbook-plugin-search/search-engine.js"></script>
        
    
        
        <script src="../../gitbook/gitbook-plugin-search/search.js"></script>
        
    
        
        <script src="../../gitbook/gitbook-plugin-lunr/lunr.min.js"></script>
        
    
        
        <script src="../../gitbook/gitbook-plugin-lunr/search-lunr.js"></script>
        
    
        
        <script src="../../gitbook/gitbook-plugin-fontsettings/fontsettings.js"></script>
        
    

    </body>
</html>

